package com.cssw.fyzb.busi.api.acct;

import com.cssw.fyzb.busi.service.acct.RedPackService;
import com.cssw.fyzb.busi.service.user.UserService;
import com.cssw.fyzb.core.eao.acct.LiveRedPackSubEao;
import com.cssw.fyzb.core.model.dodb.CmfLiveRedPack;
import com.cssw.fyzb.core.model.dodb.User;
import com.cssw.fyzb.core.model.eos.LiveRedPackSubEo;
import com.cssw.fyzb.core.model.vo.acct.LiveRedPackVo;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import com.ydn.simplequeue.SimpleQueue;
import com.ydn.simplequeue.SimpleQueueFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * @Author Jian Qin.
 */
@Function(description = "查询直播间红包", parameters = {
        @Parameter(name = "liveId", type = Type.Integer, description = "直播间标识", required = true),
        @Parameter(name = "userId", type = Type.Integer, description = "用户标识", required = true)
})
@Component
public class QueryLiveRedpack implements Action {
    @Autowired
    private RedPackService redPackService;

    @Autowired
    private SimpleQueueFactory simpleQueueFactory;

    @Autowired
    private UserService userService;

    @Autowired
    private LiveRedPackSubEao liveRedPackSubEao;

    @Override
    public Response execute(Request request) throws Exception {
        CmfLiveRedPack pack = redPackService.queryLatestLivePack(request.getLong("liveId"));
        if(pack == null){
            return Response.success();
        }
        LiveRedPackVo vo = new LiveRedPackVo();

        if(pack != null){
            Long redPackId = pack.getRedPackId();

            String topic = "redpack_" + redPackId;
            SimpleQueue simpleQueue = simpleQueueFactory.getQueue(topic);

            vo.setIsTake("0");
            if(simpleQueue.size() > 0){
                vo.setStatus("1");
                LiveRedPackSubEo eo = liveRedPackSubEao.queryOne(redPackId, request.getLong("userId"));
                if(eo != null){
                    vo.setIsTake("1");
                }
            }else{
                vo.setStatus("2");
            }

            User user = userService.getById(pack.getFromUserId());
            vo.setAvatar(user.getAvatar());
            vo.setNickName(user.getNickName());
            vo.setNum(pack.getNum());
            vo.setReceiveNum(pack.getNum() - simpleQueue.size());
            vo.setEffTime(pack.getEffTime());
            vo.setFromUserId(pack.getFromUserId());
            vo.setLiveId(pack.getLiveId());
            vo.setRedPackId(pack.getRedPackId());
            vo.setSendTime(pack.getSendTime());
            vo.setPackRemark(pack.getTitle());

        }


        return Response.success().put("data", vo);
    }
}
